package org.dtrd.modules.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.util.DateUtils;
import org.dtrd.modules.patient.entity.bean.report.SigninRateLine;

import javax.annotation.Nullable;
import java.util.*;

/**
 * @author qr
 * @date 2022/4/2 17:55
 */
@Slf4j
public class ReportUtil {

    public static String getReportWeekName(String packageName) {
        // 查找最新的周报
        DateTime now = DateTime.now();
        // 设置报告名字
        return String.format(packageName + "%d年%d月第%s周报告", now.getField(DateField.YEAR), now.getField(DateField.MONTH) + 1, num2Chinese(now.getField(DateField.WEEK_OF_MONTH)));
    }

    public static String getReportMonthName(String packageName) {
        // 查找最新的周报
        DateTime now = DateTime.now();
        // 设置报告名字
        return String.format(packageName + "%d年第%s月报告", now.getField(DateField.YEAR), now.getField(DateField.MONTH) + 1);
    }

    /**
     * 一个月最多5周
     */
    private static String num2Chinese(Integer num) {
        String chinese = null;
        switch (num) {
            case 1:
                chinese = "一";
                break;
            case 2:
                chinese = "二";
                break;
            case 3:
                chinese = "三";
                break;
            case 4:
                chinese = "四";
                break;
            case 5:
                chinese = "五";
                break;
        }
        return chinese;
    }

    public static SigninRateLine getFinishedAndReachStandardRate(Date startDate, Date endDate, @Nullable Map<Date, Integer> finishedRateMap, @Nullable Map<Date, Integer> reachStandardMap) {
        List<Date> dates = DateUtils.getDateList(startDate, endDate);
        List<String> dateStrs = new ArrayList<>(dates.size());
        List<String> finishedRateStrs = new ArrayList<>(dates.size());
        List<String> reachStandardStrs = new ArrayList<>(dates.size());
        for (Date date : dates) {
            dateStrs.add(DateUtils.formatDate(date, "MM-dd"));
            // 完成率
            finishedRateStrs.add(Optional.ofNullable(finishedRateMap).map(map -> map.get(date) + "").orElse("0"));
            // 达标率
            reachStandardStrs.add(Optional.ofNullable(reachStandardMap).map(map -> map.get(date) + "").orElse("0"));
        }
        log.info("ReportUtil.getFinishedAndReachStandardRate.[startDate, endDate, finishedRateMap, reachStandardMap] " + finishedRateStrs);
        // 计划, 打卡率, 达标率
        SigninRateLine line = new SigninRateLine();
        line.setRate(dateStrs);
        line.setPunchRate(finishedRateStrs);
        line.setComplianceRate(reachStandardStrs);
        List<List<String>> dataList = new ArrayList<>(2);
        dataList.add(finishedRateStrs);
        dataList.add(reachStandardStrs);
        line.setDataList(dataList);
        return line;
    }
}
